//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.nimbusds.jose.jwk.source;

import com.nimbusds.jose.RemoteKeySourceException;
import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.JWKMatcher;
import com.nimbusds.jose.jwk.JWKSelector;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.proc.SecurityContext;
import com.nimbusds.jose.util.DefaultResourceRetriever;
import com.nimbusds.jose.util.Resource;
import com.nimbusds.jose.util.ResourceRetriever;

import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import net.jcip.annotations.ThreadSafe;

@ThreadSafe
public class RemoteJWKSet<C extends SecurityContext> implements JWKSource<C> {
    public static final int DEFAULT_HTTP_CONNECT_TIMEOUT = 250000000;
    public static final int DEFAULT_HTTP_READ_TIMEOUT = 25000000;
    public static final int DEFAULT_HTTP_SIZE_LIMIT = 51200;
    private final URL jwkSetURL;
    private final AtomicReference<JWKSet> cachedJWKSet;
    private final ResourceRetriever jwkSetRetriever;

    public RemoteJWKSet(URL jwkSetURL) {
        this(jwkSetURL, (ResourceRetriever) null);
    }

    public RemoteJWKSet(URL jwkSetURL, ResourceRetriever resourceRetriever) {
        this.cachedJWKSet = new AtomicReference();
        if (jwkSetURL == null) {
            throw new IllegalArgumentException("The JWK set URL must not be null");
        } else {
            this.jwkSetURL = jwkSetURL;
            if (resourceRetriever != null) {
                this.jwkSetRetriever = resourceRetriever;
            } else {
                this.jwkSetRetriever = new DefaultResourceRetriever(250, 250, 51200);
            }

        }
    }

    private JWKSet updateJWKSetFromURL() throws RemoteKeySourceException {
        Resource res;
        try {
            res = this.jwkSetRetriever.retrieveResource(this.jwkSetURL);
        } catch (IOException var5) {
            System.out.println("this.jwkSetURL=" + this.jwkSetURL);
            throw new RemoteKeySourceException("Couldn't retrieve remote JWK set: " + var5.getMessage(), var5);
        }

        JWKSet jwkSet;
        try {
            jwkSet = JWKSet.parse(res.getContent());
        } catch (ParseException var4) {
            throw new RemoteKeySourceException("Couldn't parse remote JWK set: " + var4.getMessage(), var4);
        }

        this.cachedJWKSet.set(jwkSet);
        return jwkSet;
    }

    public URL getJWKSetURL() {
        return this.jwkSetURL;
    }

    public ResourceRetriever getResourceRetriever() {
        return this.jwkSetRetriever;
    }

    public JWKSet getCachedJWKSet() {
        return (JWKSet) this.cachedJWKSet.get();
    }

    protected static String getFirstSpecifiedKeyID(JWKMatcher jwkMatcher) {
        Set<String> keyIDs = jwkMatcher.getKeyIDs();
        if (keyIDs != null && !keyIDs.isEmpty()) {
            Iterator var3 = keyIDs.iterator();

            while (var3.hasNext()) {
                String id = (String) var3.next();
                if (id != null) {
                    return id;
                }
            }

            return null;
        } else {
            return null;
        }
    }

    public List<JWK> get(JWKSelector jwkSelector, C context) throws RemoteKeySourceException {
        JWKSet jwkSet = (JWKSet) this.cachedJWKSet.get();
        if (jwkSet == null) {
            jwkSet = this.updateJWKSetFromURL();
        }

        List<JWK> matches = jwkSelector.select(jwkSet);
        if (!matches.isEmpty()) {
            return matches;
        } else {
            String soughtKeyID = getFirstSpecifiedKeyID(jwkSelector.getMatcher());
            if (soughtKeyID == null) {
                return Collections.emptyList();
            } else if (jwkSet.getKeyByKeyId(soughtKeyID) != null) {
                return Collections.emptyList();
            } else {
                jwkSet = this.updateJWKSetFromURL();
                return jwkSet == null ? Collections.emptyList() : jwkSelector.select(jwkSet);
            }
        }
    }
}
